package org.jivesoftware.spark.crowd.packet;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.crowd.Param;
import org.jivesoftware.spark.crowd.packet.MParser.MParserProvider;
import org.xmlpull.v1.XmlPullParser;

public class DParser implements PacketExtension{
	private String en = "";
	private String xmlns = "";
	private String crowdName = "";
	@Override
	public String getElementName() {
		// TODO Auto-generated method stub
		return en;
	}

	@Override
	public String getNamespace() {
		// TODO Auto-generated method stub
		return xmlns;
	}
	
	public void setEn(String en) {
		this.en = en;
	}

	public void setXmlns(String xmlns) {
		this.xmlns = xmlns;
	}
	
	
	public String getCrowdName() {
		return crowdName;
	}

	public void setCrowdName(String crowdName) {
		this.crowdName = crowdName;
	}

	@Override
	public String toXML() {
		StringBuilder sb = new StringBuilder();
		sb.append("<")
		.append(Param.CROWD_SERVICE_NAME)
		.append(" xmlns=\"")
		.append(xmlns)
		.append("\"")
		.append(" crowdname=\"")
		.append(crowdName)
		.append("\"/>");

		return sb.toString();
	}
	
	public static class DParserProvider implements PacketExtensionProvider{
		private static Object LOCK = new Object();
		private static DParserProvider instance;
		public static DParserProvider getInstance(){
			synchronized(LOCK){
			if(instance == null)
				instance = new DParserProvider();
			}
			return instance;
		}
		private DParserProvider(){
			ProviderManager.getInstance().addExtensionProvider(Param.CROWD_SERVICE_NAME, Param.destroy_NS, this);
		}
		@Override
		public PacketExtension parseExtension(XmlPullParser parser)
				throws Exception {
			DParser dParser = new DParser();
			dParser.setCrowdName(parser.getAttributeValue("", "crowdname"));
			dParser.setEn(parser.getName());
			dParser.setXmlns(parser.getNamespace());
			return dParser;
		}
		
	}
	

	

	


}
